package essential.ch3;

import java.util.Vector;

/**
 * @Date 2021/10/23
 * @Created by Jonathan
 */
public class AbnormalResult {
    public static void main(String[] args) {
        Vector<A> v = new Vector<>();
        A a = new A();
        B b = new B();
        v.add(a);
        System.out.printf("v.contains(a):%s\n", v.contains(a));
        System.out.printf("v.contains(b):%s\n", v.contains(b));
        v.clear();
        v.add(b);
        System.out.printf("v.contains(b):%s\n", v.contains(b));
        System.out.printf("v.contains(a):%s\n", v.contains(a));

    }
}

class A {
    @Override
    public boolean equals(Object obj) {
        return obj instanceof A;
    }
}

/**
 * 实现了Map接口的类会使用到键对象（作为key的对象）的哈希码，当调用put方法加入键值对或调用get方法取回值对象的时候，
 * 都是根据键对象的哈希码来计算存储位置的。如果对象的哈希码没有相关保证，就不能够得到预期的结果
 */
class B extends A {
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof B) {
            return true;
        }
        return super.equals(obj);
    }
}
